package com.briup.bms.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.briup.bms.common.exceptions.BmsServiceException;
import com.briup.bms.common.utils.ResultCode;
import com.briup.bms.dao.TitleMapper;
import com.briup.bms.service.ITitleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
public class TitleServiceImpl implements ITitleService {
	@Autowired
	private TitleMapper titleMapper;
	@Override
	public void saveTitle(String title) {
		if(title==null||"".equals(title)) {
			throw new BmsServiceException(ResultCode.PARAM_NOT_EXIST,"职称不能为空！");
		}
		String tiResult = titleMapper.selectByName(title);
		if(tiResult!=null &&!"".equals(tiResult)) {//只要不为空，那么数据库中已经存在这个title，不能继续插入
			throw new BmsServiceException(ResultCode.DATA_EXIST, "该职称已经存在！");
		}
		titleMapper.insert(title);
	}
	@Override
	public PageInfo<Map<String, String>> findByCondition(int pageNum, int pageSize, String title) {
		title = (title==null||"".equals(title))?title:"%"+title+"%";
		PageHelper.startPage(pageNum,pageSize);
		List<Map<String,String>> list = titleMapper.findByNameLike(title);
		return new PageInfo<>(list);
	}
	
	@Override
	public void deleteTitle(String title) {
		//根据职称查找员工信息，是否该职称下有员工。
		//以抛异常的形式提示用户，应该先接触和该职称关联的数据信息，然后再删除职称。
		
		int count = titleMapper.delete(title);
//		if(count==0) {
//			throw new BmsServiceException(ResultCode.DATA_NOT_EXIST, "要删除的职位不存在！");
//		}
	}

}
